<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增客服</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<body>
<div style="width:80%;height:100%;margin:15px auto;overflow:auto;">
    <form class="layui-form" action="">
        <div class="layui-form-item" id="title_div">
            <label class="layui-form-label" >客服名</label>
            <div class="layui-input-block">
                <input type="text" name="kefuName" required  lay-verify="required" placeholder="请输入客服名" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" id="message_div">
            <label class="layui-form-label" >密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" required  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属区域</label>
            <div class="layui-input-block">
                <select lay-search id="areaId" name="areaId" required  lay-verify="required">
                    <option id="areas" disabled selected></option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" >状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><span >启用</span></div>
                <input type="radio" name="status" value="0" title="禁用"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i><span >禁用</span></div>
            </div>
        </div>


        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye" >立即添加</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary" >重置</button>
            </div>
        </div>
    </form>
</div>
</body>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>
<script>
    var $ = layui.jquery;


    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;

        //遍历下选框
        $.ajax({
            url: "/area/getAllArea"
            , type: "get"
            , contentType: 'application/json'
            , success: function (data) {
                $.each(data.data, function (index, item) {
                    $('#areaId').append(new Option(item.areaName, item.id));// 下拉菜单里添加元素
                });
                layui.form.render("select");
            }
        });

        //添加操作
        form.on('submit(addWuye)',function(data){
            var kefuName = $("input[name='kefuName']").val();
            var password = $("input[name='password']").val();
            var areaId = $("#areaId").val();

            if (kefuName=='' || kefuName==null){
                layer.msg("请填写客服名");
            }else if (password=='' || password==null){
                layer.msg("请填写密码");
            }else if (areaId==null || areaId==""){
                layer.msg("请选择区域");
            }else{
                var msgMovie = layer.load(1);
                $.ajax({
                    url:"/kefu/addKefu"
                    ,type:"POST"
                    ,contentType: 'application/json'
                    ,data:JSON.stringify(data.field)
                    ,success:function (data) {
                        if(data.code==200){
                            layer.close(layer.index);
                            layer.msg('添加成功');
                            setTimeout(function(){
                                window.location.reload();
                            },2000);
                        }else {
                            layer.msg(data.msg,{icon:2,time:2000})
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                });
                layer.close(msgMovie);
            }
            return false;
        });

    });
</script>
</html>